package revir;
import java.util.*;
import java.util.stream.Stream;
public class optional {
        public static void main(String[] args) {
            StudentsStream.stream()
                    .limit(5)
                    .filter(Optional::isPresent)
                    .filter(o->o.get().getAGE()>18)
                    .map(Optional::get)
                    .forEach(System.out::println);
        }

        public static class StudentsStream extends Student {
            static List<Student> name = Arrays.asList(
                    new Student("森七菜", "5", 21),
                    new Student("宣美", "2", 23),
                    new Student("奈绪", "3", 15),
                    new Student("允雅", "8", 20)
            );
            static Random random = new Random();

            public StudentsStream(String NAME, String STUDENT_ID, int AGE) {
                super(NAME, STUDENT_ID, AGE);
            }

            public static Student NewStu() {
                int num = random.nextInt(3);
                int index = random.nextInt(name.size());
                switch (num) {
                    case 0:
                        return name.get(index);
                    case 1:
                        return name.get(index);
                    case 2:
                        return name.get(index);
                    default:
                        return null;
                }

            }

            public static Stream<Optional<Student>> stream() {
                return Stream.generate(StudentsStream::NewStu)
                        .map(student -> Optional.ofNullable(student));
            }
        }

}


